/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package ant.antsys;
import java.io.File;
import java.net.URL;


import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.XSLTProcess;
import org.apache.tools.ant.taskdefs.Delete;
import org.apache.tools.ant.taskdefs.TempFile;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.resources.URLResource;


/**
 * Generate HTML report 
 *
 * @ant.task name="astreport" category="testing"
 */
class HtmlReport extends Task {
	/**
	 * name of the frames format.
	 */
	public static final String FRAMES = "frames";

	/**
	 * name of the no frames format.
	 */
	public static final String NOFRAMES = "noframes";

	/**
	 * Used to ensure the uniqueness of a property
	 */
	private static int counter = 0;

	File xmlFile;
	File toDir;
	Task task;
	String format = NOFRAMES;

	public HtmlReport(Task task) {
		this.task = task;
	}

	public void setXmlFile(File xmlFile) {
		this.xmlFile = xmlFile;
	}

	public void setToDir(File toDir) {
		this.toDir = toDir;
	}

	public void execute() throws BuildException {
		transform();
	}

	/**
	 * access the stylesheet to be used as a resource.
	 * @return stylesheet as a resource
	 */
	protected Resource getStylesheet() {
		String xslname = "junit-frames.xsl";
		if (NOFRAMES.equals(format)) {
			xslname = "junit-noframes.xsl";
		}

		URLResource stylesheet = new URLResource();
		URL stylesheetURL = null;
		stylesheetURL = getClass().getClassLoader().getResource(
				"ant/antsys/xsl/" + xslname);
		stylesheet.setURL(stylesheetURL);
		return stylesheet;
	}

	protected void transform() {
		Project project = task.getProject();

		TempFile tempFileTask = new TempFile();
		tempFileTask.bindToOwner(task);

		XSLTProcess xsltTask = new XSLTProcess();
		xsltTask.bindToOwner(task);

		xsltTask.setXslResource(getStylesheet());

		xsltTask.setIn(xmlFile);
		File outputFile = null;
		if (format.equals(FRAMES)) {
			String tempFileProperty = getClass().getName()
					+ String.valueOf(counter++);
			File tmp = FileUtils.getFileUtils()
					.resolveFile(project.getBaseDir(),
							project.getProperty("java.io.tmpdir"));
			tempFileTask.setDestDir(tmp);
			tempFileTask.setProperty(tempFileProperty);
			tempFileTask.execute();
			outputFile = new File(project.getProperty(tempFileProperty));
		} else {
			outputFile = new File(toDir, "systemtestresult.html");
		}
		xsltTask.setOut(outputFile);
		/*
		for (Iterator i = params.iterator(); i.hasNext();) {
		    XSLTProcess.Param param = (XSLTProcess.Param) i.next();
		    XSLTProcess.Param newParam = xsltTask.createParam();
		    newParam.setProject(task.getProject());
		    newParam.setName(param.getName());
		    newParam.setExpression(param.getExpression());
		} */
		XSLTProcess.Param paramx = xsltTask.createParam();
		paramx.setProject(task.getProject());
		paramx.setName("output.dir");
		paramx.setExpression(toDir.getAbsolutePath());
		final long t0 = System.currentTimeMillis();
		try {
			xsltTask.execute();
		} catch (Exception e) {
			throw new BuildException("Errors while applying transformations: "
					+ e.getMessage(), e);
		}
		final long dt = System.currentTimeMillis() - t0;
		task.log("Transform time: " + dt + "ms");
		if (format.equals(FRAMES)) {
			Delete delete = new Delete();
			delete.bindToOwner(task);
			delete.setFile(outputFile);
			delete.execute();
		}
	}

}
